import apps from "./app";
import {
  registerMicroApps,
  addGlobalUncaughtErrorHandler,
  start,
  initGlobalState,
} from "qiankun/dist/index.umd.js";

// 注册微应用基础信息
registerMicroApps(apps, {
  beforeLoad: (app) => {
    console.log("before load app.name====>>>>>", app.name);
  },
  beforeMount: [
    (app) => {
      console.log("before mount app.name====>>>>>", app.name);
    },
  ],
  afterMount: [
    (app) => {
      console.log("after mount app.name====>>>>>", app.name);
    },
  ],
  afterUnmount: [
    (app) => {
      console.log("after unmount app.name====>>>>>", app.name);
    },
  ],
});

// 初始化 state
const actions = initGlobalState({});

actions.onGlobalStateChange((state, prev) => {
  // state: 变更后的状态; prev 变更前的状态
  console.log(state, prev, "onGlobalStateChangeonGlobalStateChange");
});
// actions.setGlobalState({ nihao: 111 });
// actions.offGlobalStateChange();

// 添加全局的未捕获异常处理器
addGlobalUncaughtErrorHandler((event) => {
  console.err("微应用加载失败！", event);
});

export default start;

// export default (props) => {
//   setTimeout(() => {
//     // 注册微应用基础信息
//     const app1 = apps.map((item) => {
//       return {
//         ...item,
//         props: {
//           vueUser: store.getters,
//         },
//         // 子应用挂载的div
//       };
//     });
//     registerMicroApps(app1, {
//       beforeLoad: (app) => {
//         console.log("before load app.name====>>>>>", app.name);
//       },
//       beforeMount: [
//         (app) => {
//           console.log("before mount app.name====>>>>>", app.name);
//         },
//       ],
//       afterMount: [
//         (app) => {
//           console.log("after mount app.name====>>>>>", app.name);
//         },
//       ],
//       afterUnmount: [
//         (app) => {
//           console.log("after unmount app.name====>>>>>", app.name);
//         },
//       ],
//     });

//     // 初始化 state
//     const actions = initGlobalState({});

//     actions.onGlobalStateChange((state, prev) => {
//       // state: 变更后的状态; prev 变更前的状态
//       console.log(state, prev, "onGlobalStateChangeonGlobalStateChange");
//     });
//     // actions.setGlobalState({ nihao: 111 });
//     // actions.offGlobalStateChange();

//     // 添加全局的未捕获异常处理器
//     addGlobalUncaughtErrorHandler((event) => {
//       console.err("微应用加载失败！", event);
//     });

//     start(props);
//   });
// };
